/**
 * Created by 黄二杰 on 2016/3/11.
 */
"use strict";
var mysql = require('mysql2/promise');


var usePool = true;




//开发服
var config = {
    host: 'localhost',
    user: 'root',
    password: '99914848',
    database: 'buyudb',
    port: 3306
};



var pool;
var x_query;
if (usePool) {
    pool = mysql.createPool({
        connectionLimit: 20,
        host: config.host,
        user: config.user,
        password: config.password,
        database: config.database,
        port: config.port
    });
    x_query = function (sql, val) {


        return pool.getConnection().then((conn) => {
            var res = conn.query(sql, val);
            conn.release();
            return res;
        }).catch(err=> {
            console.error("---------------------------------------------------------");
            console.error(err);
            console.error(sql);
            console.error(val);
            console.error("---------------------------------------------------------");
        });
    };
}
else {
    pool = {};
    x_query = function (sql, val) {

        return mysql.createConnection(config)
            .then((conn) => conn.query(sql, val))
            .catch(err=> {
                console.error("---------------------------------------------------------");
                console.error(err);
                console.error(sql);
                console.error(val);
                console.error("---------------------------------------------------------");
            });
    };
}


pool.x_query = x_query;


//对外开放接口
module.exports = pool;
